<?php

namespace app\shop\controller\question;

use app\shop\model\course\Category as CategoryModel;
use app\shop\model\question\Question as QuestionModel;
use app\shop\controller\Controller;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

/**
 * 讲师管理控制器
 */
class Question extends Controller
{
    /**
     * 讲师列表(全部)
     */

    /*
   * 导入文件
   * */
    public function import()
    {
        // 获取上传的文件
        $file = request()->file('file');

        if (!$file) {
            return $this->renderError('未上传 Excel 文件');
        }


        // 读取 Excel 文件
        $spreadsheet = IOFactory::load($file->getRealPath());
        $worksheet = $spreadsheet->getActiveSheet();
        $highestRow = $worksheet->getHighestRow();
        $highestColumn = $worksheet->getHighestColumn();
        if ($highestRow > 2001) {
            return $this->renderError('最大只支持两千条数据！');
        }
        $data = [];
        $CategoryModel = new CategoryModel;
        $catelist = $CategoryModel->select()->toArray();
        $QuestionModel = new QuestionModel;
        // 遍历每一行数据
        for ($row = 2; $row <= $highestRow; $row++) {
            $rowData = [];
            // 遍历每一列数据
            for ($col = 'A'; $col <= $highestColumn; $col++) {
                $cellValue = $worksheet->getCell($col . $row)->getValue();
                $rowData[] = $cellValue;
            }
            $data[] = $rowData;
        }
        $list = [];
        foreach ($data as $k => $v) {

            $arr = [];
            //类型
            switch ($v[0]) {
                case "判断题";
                    $arr['question_type'] = 3;
                    break;
                case "单选题";
                    $arr['question_type'] = 1;
                    break;
                case "多选题";
                    $arr['question_type'] = 2;
                    break;
            }
            //分类

            $Category_name = $v[1];
            $result = array_filter($catelist, function ($item) use ($Category_name) {
                return $item['name'] == $Category_name;
            });

            if (empty($result)) {
                $arr = [];
                continue;
            } else {
                foreach ($result as $k1 => $v1) {
                    $arr['category_id'] = $v1['category_id'];
                    break;
                }

            }
            //答案
            $arr['title'] = $v[2];
            $arr['answer'] = $v[3];

            $arr['difficulty'] = $v[4];
            $arr['sort'] = $v[5];
            $arr['analysis'] = $v[6];
            $option = [];
            for ($i = 6; $i < 15; $i++) {
                if (!empty($v[$i])) {
                    $option[] = ['content' => $v[$i], 'type' => 1];
                } else {
                    break;
                }
            }

            $arr['option'] = json_encode($option, true);
            $arr['app_id'] = 10001;
            $arr['create_time'] = time();
            $list[] = $arr;
        }
        $result = $QuestionModel->addarr($list);
        if ($result) {
            return $this->renderSuccess('文件导入成功');
        }
        return $this->renderError('文件导入失败!');

    }

    public function index()
    {
        // 获取全部列表
        $model = new QuestionModel;
        $list = $model->getList($this->postData());
        // 分类
        $category = CategoryModel::getCacheTree();
        return $this->renderSuccess('', compact('list', 'category'));
    }

    /**
     * 添加
     */
    public function add()
    {
        $model = new QuestionModel;
        // get请求
        if ($this->request->isGet()) {
            // 分类
            $category = CategoryModel::getCacheTree();
            $questionType = $model::questionType();
            return $this->renderSuccess('', compact('category', 'questionType'));
        }
        //post请求
        if ($model->add($this->postData())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑
     */
    public function edit($question_id)
    {
        $model = QuestionModel::detail($question_id);
        if ($this->request->isGet()) {
            // 分类
            $category = CategoryModel::getCacheTree();
            $questionType = $model::questionType();
            return $this->renderSuccess('', compact('category', 'model', 'questionType'));
        }
        // 更新记录
        if ($model->edit($this->postData())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 修改状态
     */
    public function state($question_id, $state)
    {
        // 商品详情
        $model = QuestionModel::detail($question_id);
        if (!$model->setStatus($state)) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess('操作成功');
    }

    /**
     * 设置推荐
     */
    public function settop($question_id, $state)
    {
        // 详情
        $model = QuestionModel::detail($question_id);
        if (!$model->setTop($state)) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess('操作成功');
    }

    /**
     * 删除
     */
    public function delete($question_id)
    {
        // 详情
        $model = QuestionModel::detail($question_id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

}
